package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.BuyOftenGoodsBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.util.RoundedFormation;
import cn.xfdzx.android.apps.shop.util.StringUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.TagTextView;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：我的界面常买
 */
public class OftenBuyAdapter extends RecyclerView.Adapter<OftenBuyAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<BuyOftenGoodsBeen.DataBean> mList;
    private Context mContext;
    private int proportion;


    public OftenBuyAdapter(Context context, List<BuyOftenGoodsBeen.DataBean> list) {
        this.mList = list;
        this.mContext = context;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_often_buy, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        if (proportion!=0){
            proportion=holder.ivFineSelect.getWidth()/holder.ivFineSelect.getHeight();
        }
        //顶部左边圆角
        RoundedFormation transformation = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_LEFT);
        //顶部右边圆角
        RoundedFormation transformation1 = new RoundedFormation
                (10, 0, RoundedFormation.CornerType.TOP_RIGHT);
        //组合各种Transformation,
        MultiTransformation<Bitmap> mation = new MultiTransformation<>
                //Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法,将new CenterCrop()添加至此
                (new CenterCrop(), transformation, transformation1);
        RequestOptions options = RequestOptions.bitmapTransform(mation).override(200*proportion, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback( R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(mation)).into(holder.ivFineSelect);
        if ( TYPE_IS_RETAIL==0){
            holder.ttvContent.setSingleTagAndContent("", mList.get(position).getTitle());

        }else {
            holder.ttvContent.setSingleTagAndContent("", mList.get(position).getTitle());
        }

        holder.tv_maozhong.setText("毛重: " + mList.get(position).getGoods_weight() + "斤");

        if (null != mList.get(position).getUnit()) {
            if (mList.get(position).getUnit().isEmpty()) {
                if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
                    holder.tvPriceBox.setText("¥"+mList.get(position).getMarketprice());
                    holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getMarketprice(),mList.get(position).getGoods_weight())+ "/斤");

                } else {
                    holder.tvPriceBox.setText("¥"+mList.get(position).getDiscountprice());
                    holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getDiscountprice(),mList.get(position).getGoods_weight())+ "/斤");

                }
            } else {
                if (Double.parseDouble(mList.get(position).getDiscountprice()) == 0) {
                    holder.tvPriceBox.setText("¥"+mList.get(position).getMarketprice()+"/"+mList.get(position).getUnit());
                    holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getMarketprice(),mList.get(position).getGoods_weight())+ "/斤");

                } else {
                    holder.tvPriceBox.setText("¥"+mList.get(position).getDiscountprice()+"/"+mList.get(position).getUnit());
                    holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getDiscountprice(),mList.get(position).getGoods_weight())+ "/斤");

                }
            }
            holder.tvSaleNum.setText("已售" + mList.get(position).getSale_num() + "件");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        mItemClickListener.onItemClick(position);
                    }
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_fine_select)
        ImageView ivFineSelect;
        @BindView(R.id.tv_content)
        TagTextView ttvContent;
        @BindView(R.id.tv_price_box)
        TextView tvPriceBox;
        @BindView(R.id.tv_sale_num)
        TextView tvSaleNum;
        @BindView(R.id.tv_aboutweight)
        TextView tv_aboutweight;
        @BindView(R.id.tv_maozhong)
        TextView tv_maozhong;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
